import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import {resolve} from "path";
import {createSvgIconsPlugin} from "vite-plugin-svg-icons";
import { splitVendorChunkPlugin } from 'vite'
import viteCompression from 'vite-plugin-compression'

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        createSvgIconsPlugin({
            iconDirs: [resolve(process.cwd(), "src/assets/icons")],
            symbolId: "icon-[dir]-[name]"
        }),
        splitVendorChunkPlugin(),
        viteCompression({
            verbose: true,
            disable: false,
            threshold: 10240, // 对超过10k的文件进行压缩
            algorithm: 'gzip',
            ext: '.gz',
        }),
    ],

    resolve: {
        // 配置路径别名
        alias: {
            '@': resolve(__dirname, "./src"),
            "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
        },
    },

    //引入全局SCSS文件
    css: {
        preprocessorOptions: {
            scss: {
                /**如果引入多个文件，可以使用
                 * '@import "@/assets/scss/globalVariable1.scss";
                 * @import"@/assets/scss/globalVariable2.scss";'
                 **/
                additionalData: '@import "@/styles/var.scss";', //引入scss文件
            }
        }
    },

    server: {
        host: "0.0.0.0",
        port: 8070,
        //启动时，自动打开浏览器
        open: '/',
        // cors跨域
        cors: true,
    },
    optimizeDeps: {
        include: ['lodash', 'axios'], // 需要预Bundle的依赖
    },
    // 缩小打包的js体积
    build: {
        target: 'es2015', // 设置目标环境的JavaScript版本
        outDir: 'dist', // 构建时输出目录
        assetsDir: 'assets', // 放置生成的静态资源的目录
        minify: 'terser', // 构建时压缩
        terserOptions: {
            compress: {
                drop_console: true, // 生产环境下去除console
            },
        },
        chunkSizeWarningLimit: 2000, //  chunk大小警告的限制
        rollupOptions: {
            output: {
                // 分包
                manualChunks(id) {
                    if (id.includes('node_modules')) {
                        return id.toString().split('node_modules/')[1].split('/')[0].toString()
                    }
                }
            }
        }
    },
})
